package me.zhanshi123.lab4.service.impl;

import me.zhanshi123.lab4.entity.ApiResponse;
import me.zhanshi123.lab4.entity.User;
import me.zhanshi123.lab4.jwt.JwtUtils;
import me.zhanshi123.lab4.service.IApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ApiService implements IApiService {
    @Autowired
    private UserService userService;

    @Autowired
    private JwtUtils jwtUtils;

    public String login(String name, String password) {
        ApiResponse<String> response = new ApiResponse<>();
        User user = userService.getUser(name);
        if (user == null) {
            return response.fail("用户名或密码不匹配");
        }
        boolean suc = userService.checkPassword(user, password);
        if (!suc) {
            return response.fail("用户名或密码不匹配");
        }
        return response.success(jwtUtils.createJwt(user, 86400L * 10000 * 7));
    }
}
